//
//  BHMeServiceManager.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/8/10.
//

import UIKit

class BHMeServiceManager {
    
    var responseUserInfoBlock:((BHUserPageDataModel) -> Void)?
    

    // 获取用户资料和点赞之类的数量的接口
    func personalMainService(userId: String?) {
        
        let model = BHUserPageDataModel.init()
        
        var dic: [String: Any] = [:]
        if userId != nil {
            dic["userId"] = userId!
        }
        
        let group = DispatchGroup.init()
        group.enter()
        let infoManager = BHHTTPClient.init()
        infoManager.get(url: String.me_userInfo_url, paramters: dic) { (success, errorMsg, responseDic) in
            if success == true {
                let result = BHJsonTool.decodeDicFromDic(dic: responseDic, key: "result")
                model.parseDic(result)
            }
            group.leave()
        }
        
        
        // 获取点赞、关注等数量
        group.enter()
        let countManager = BHHTTPClient.init()
        countManager.get(url: String.me_userInfo_number_url, paramters: dic) { (success, errorMsg, responseDic) in
            if success == true {
                let result = BHJsonTool.decodeDicFromDic(dic: responseDic, key: "result")
                model.parseNum(dic: result)
            }
            group.leave()
        }
        
        group.notify(queue: DispatchQueue.main) {
            self.responseUserInfoBlock?(model)
        };
    }
    
    
    
    // 单独查询用户资料
    func queryUserInfoService(userId: String?) {
        let model = BHUserPageDataModel.init()
        var dic: [String: Any] = [:]
        if userId != nil {
            dic["userId"] = userId!
        }
        
        let infoManager = BHHTTPClient.init()
        infoManager.get(url: String.me_userInfo_url, paramters: dic) { (success, errorMsg, responseDic) in
            if success == true {
                let result = BHJsonTool.decodeDicFromDic(dic: responseDic, key: "result")
                model.parseDic(result)
            }
            self.responseUserInfoBlock?(model)
        }
    }
    
    
    // 单独查询用户点赞之类的数量
    func queryUserNumberService(userId: String?) {
        let model = BHUserPageDataModel.init()
        var dic: [String: Any] = [:]
        if userId != nil {
            dic["userId"] = userId!
        }
        
        let infoManager = BHHTTPClient.init()
        infoManager.get(url: String.me_userInfo_number_url, paramters: dic) { (success, errorMsg, responseDic) in
            if success == true {
                let result = BHJsonTool.decodeDicFromDic(dic: responseDic, key: "result")
                model.parseNum(dic: result)
            }
            self.responseUserInfoBlock?(model)
        }
    }
}
